<?php
namespace app\admin\controller;

use think\facade\View;

use app\admin\model\TemplateDetail as TemplateDetailModel;

/**
 * 内容模板管理
 * @author huwhis@163.com
 * @version   0.0.1
 */
class TemplateDetail extends Base
{
    public function index($id = 0)
    {
        $list = TemplateDetailModel::field('id,title,sort,create_time')->select();

        return View::fetch('index', [
            'list' => $list,
        ]);
    }

    public function info($id = 0)
    {
        if ($id) {
            $data = TemplateDetailModel::find($id);
        } else {
            $data = new TemplateDetailModel();
        }

        return View::fetch('save', [
            'data' => $data,
        ]);
    }

    public function save()
    {
        if ($this->app->request->isPost()) {
            $param = $this->app->request->param();

            if ($param['title'] == '') {
                $this->error("标题不能为空");
            }

            try {
                if ($param['id'] != 0) {
                    TemplateDetailModel::update($param);
                } else {
                    unset($param['id']);
                    TemplateDetailModel::create($param);
                }
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }

            $this->success('操作成功', url('template_detail/index'));
        }
    }
}
